package com.fbt.transfer.app.driver.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.fbt.transfer.app.driver.R;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Optional;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DialogUtils {

  public static final String defaultError = "网络错误，请稍后再试！";
  private static Dialog loadingDialog;
  private static Handler handler = new Handler(Looper.getMainLooper());
  private static Runnable dismissRunnable;

  public static void showInfoToast(Context context, String message) {
    ZMToast.info(context, message);
  }

  public static void showInfoToastErr(Context context, String message) {
    ZMToast.err(context, message);
  }

  public static void showTip(Context context, String message, boolean isOK) {
    ZMToast.info(context, Optional.ofNullable(message).orElse(defaultError), isOK);
  }

  public static void showOkToast(Context context, String message) {
    ZMToast.ok(context, message);
  }

  public static Dialog createLoadingDialog(Context context, boolean isClickable) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.loading_layout_wheel, null);
    Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);
    loadingDialog.setCancelable(isClickable);
    loadingDialog.setContentView(
        v,
        new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        )
    );
    return loadingDialog;
  }

  public static void showLoading(Context context, String message) {
    if (loadingDialog != null && loadingDialog.isShowing()) {
      loadingDialog.dismiss();
    }
    loadingDialog = createLoadingDialog(context, false);
    loadingDialog.show();
  }

  public static void dismissLoading() {
    if (loadingDialog != null && loadingDialog.isShowing()) {
      loadingDialog.dismiss();
      loadingDialog = null;
    }
  }

  public static Activity getCurrentActivity() {
    try {
      Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
      Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
      Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
      activitiesField.setAccessible(true);
      Object activities = activitiesField.get(activityThread);
      for (Object activityRecord : ((Map) activities).values()) {
        Class<?> activityRecordClass = activityRecord.getClass();
        Field activityField = activityRecordClass.getDeclaredField("activity");
        activityField.setAccessible(true);
        Activity activity = (Activity) activityField.get(activityRecord);
        if (activity != null && !activity.isFinishing()) {
          return activity;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  public static void showConfirmDialog(Context context, String title, String message,
      ConfirmClickListener confirmClickListener) {
    if (!(context instanceof Activity)) {
      return;
    }
    Activity activity = (Activity) context;
    if (activity.isFinishing()) {
      return;
    }
    Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(true);
    View view = LayoutInflater.from(activity).inflate(R.layout.dialog_message, null);
    TextView messageText = view.findViewById(R.id.tv_message);
    TextView tvTitle = view.findViewById(R.id.tv_title);
    tvTitle.setText(title);
    Button confirmButton = view.findViewById(R.id.btn_confirm);
    Button cancelButton = view.findViewById(R.id.btn_cancel);
    cancelButton.setOnClickListener(v -> dialog.dismiss());
    messageText.setText(message);
    confirmButton.setOnClickListener(
        v -> Optional.ofNullable(confirmClickListener).ifPresent(ConfirmClickListener::onClick));
    dialog.setContentView(view);
    dialog.show();
  }

  public interface ConfirmClickListener {

    void onClick();
  }
}
